/**
 * historyProvider module
 */

 var historyProvider = (function (){
     var history = {
         commands: [],
         stackPosition: -1,
         savePosition: -1
     };

     var clearRedo = function (){
         history.commands = history.commands.slice(0, history.stackPosition + 1);
     }

     return {
         execute: function (command){
             clearRedo();
             command.execute();
             history.commands.push(command);
             history.stackPosition++;
         },
         undo: function (){
             var command = history.commands[history.stackPosition];
             command.undo();
             history.stackPosition--;
         },
         canUndo: function (){
             return history.stackPosition >= 0;
         },
         redo: function (){
             history.stackPosition++;
             var command = history.commands[history.stackPosition];
             command.redo();
         },
         canRedo: function (){
             return history.stackPosition < history.commands.length - 1;
         },
         save: function (){
             history.savePosition = history.stackPosition;
             history.clearHistory();
         },
         dirty: function (){
             return history.stackPosition != history.savePosition;
         }
     }
 })();

 module.exports = historyProvider;
